package com.fk.regimen;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.fk.adapter.FaqItemExpandAdapter;
import com.fk.bean.Faq;
import com.fk.util.Net;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.TextView;

public class FaqdetailActivity extends Activity implements OnClickListener{
	
	Button btn_toFaqFragment;
	TextView tv_faqdescrib;
	ExpandableListView exlist_faqproblem;
	Faq faq = new Faq();
	String faq_describ;
	
	//Adapter中子视图显示文字
	private String[][] generals;

	String tag = "china";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.faqdetail);
		
		btn_toFaqFragment = (Button) findViewById(R.id.btn_toFaqFragment);
		tv_faqdescrib = (TextView) findViewById(R.id.tv_faqdescrib);
		exlist_faqproblem = (ExpandableListView) findViewById(R.id.exlist_faqproblem);
		
		btn_toFaqFragment.setOnClickListener(FaqdetailActivity.this);
		
		//从网络获取症状详情数据
		getFaqDetails();
		
		
		Log.e(tag, "得到原因："+faq.getFaq_reason());
		
		
	}

	
	
	private void getFaqDetails() {
		
		Intent intent = getIntent();
		String faq_name = intent.getStringExtra("faq_name");
		
		HttpUtils httpUtils = new HttpUtils();
		
		RequestParams params = new RequestParams();
		params.addBodyParameter("faq_name", faq_name);
		
		String webPath = Net.getIP(FaqdetailActivity.this);
		httpUtils.send(HttpMethod.POST, webPath+"FaqServlet", params, new RequestCallBack<String>() {

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				// TODO Auto-generated method stub
				Log.e(tag, "木成功"+arg1);
			}

			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				// TODO Auto-generated method stub
				Log.e(tag, "成功喽:"+arg0.toString());
				
				
				try {
					
					/*
					 * 返回时Jsonobject：
						JSONObject jsonObject = new JSONObject(responseInfo.result);
					         返回时JsonArray：
					   JSONArray jsonArray = new JSONArray(responseInfo.result);
					 */
					JSONObject jObject = new JSONObject(arg0.result);
					//这个很重要
					JSONObject jsonObject = jObject.getJSONObject("result");
					
					faq.setFaq_name(jsonObject.getString("faq_name"));
					faq.setFaq_describ(jsonObject.getString("faq_describ"));
					faq.setFaq_reason(jsonObject.getString("faq_reason"));
					faq.setFaq_doctor(jsonObject.getString("faq_doctor"));
					faq.setFaq_advice(jsonObject.getString("faq_advice"));
					Log.e(tag, "json的何时就医-----》"+jsonObject.getString("faq_doctor"));
					
					Log.e(tag, "第一次得到信息："+faq.getFaq_doctor());
					tv_faqdescrib.setText(faq.getFaq_describ());
					//Adapter中子视图显示文字
					generals = new String[][]{
							{faq.getFaq_reason()},
							{faq.getFaq_doctor()},
							{faq.getFaq_advice()}	
					};
					
					FaqItemExpandAdapter adapter = new FaqItemExpandAdapter(FaqdetailActivity.this,generals);
					
					exlist_faqproblem.setAdapter(adapter);
					
					
				} catch (JSONException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				
			}

			
		});
		
		
	}



	@Override
	public void onClick(View v) {
		//
		switch (v.getId()) {
		case R.id.btn_toFaqFragment:
			//点击了返回按钮，返回常见问题碎片??
			
			break;

		default:
			break;
		}
		
	}
	


	
}
